<template>
  <div class="w-full h-screen">
    <my-list ref="paging" v-model="state.listData" @onLoad="getList">
      <Waterfall :list="state.listData" :breakpoints="{750:{rowPerView:2}}" backgroundColor="#f5f5f5">
        <!-- 新版插槽数据获取 -->
        <template #default="{ item, url, index }">
          <div class="ad h-300 flex align-items-center justify-center bg-warming mt-10"
               v-if="route.query.a && (index + 1) % 3 === 0">
            这是url有A参数的广告位置
          </div>
          <div class="ad h-300 flex align-items-center justify-center bg-warming mt-10"
               v-else-if="!route.query.a && (index + 1) % 6 === 0">
            这是url没有A参数的广告位置
          </div>

          <div class="card bg-white mt-10">
            <LazyImg class="card_img" :url="url" v-if="item.isImg"/>
            <video :src="url" controls class="card_video" v-else></video>
            <div class="p-10">
              <div class="f-16">{{ item.title }}</div>
              <div class="f-12 text-999 mt-10">{{ item.desc }}</div>
              <div class="flex justif-between align-items-center f-12 text-999 mt-10">
                <div class="flex align-items-center">
                  <img :src="item.avatar" class="w-20 h-20 radius-circle"/>
                  <span class="ml-5">{{ item.auth }}</span>
                </div>
                <div class="flex align-items-center">
                  <img :src="like" class="w-14 h-14 radius-circle"/>
                  <span class="ml-5">{{ item.like }}</span>
                </div>
              </div>
            </div>
          </div>
        </template>
      </Waterfall>
    </my-list>
  </div>
</template>

<script setup lang="ts">
import axios from 'axios'
import {ref, reactive, onMounted} from 'vue';
import {useRouter, useRoute} from 'vue-router';
import myList from '@/components/Z-UI/ZList/myList.vue';
// 瀑布流插件
import {LazyImg, Waterfall} from 'vue-waterfall-plugin-next'
import 'vue-waterfall-plugin-next/dist/style.css'
import like from '/like.png'
import avatar from '/avatar.webp'
import img1 from '/1.jfif'
import img2 from '/2.jfif'
import img3 from '/3.jfif'
import img4 from '/4.jfif'
import img5 from '/5.jfif'
import img6 from '/6.gif'
import img7 from '/7.gif'
import img8 from '/8.jfif'
import img9 from '/9.jfif'
import img10 from '/10.mp4'

const images = [img1, img2, img3, img4, img5, img6, img7, img8, img9, img10]

// mock
import {generateMockData, paginateData, mockApiRequest} from '@/utils/mockData.js'
// 生成100条虚拟数据
const allMockData = generateMockData(100)

const route = useRoute()

// 模拟API请求
const paging = ref(null)
const state = reactive({
  queryParams: {},
  listData: []
})

const getList = async ({pageNo, pageSize}) => {
  // console.log(`请求第${pageNo}页，每页${pageSize}条`);
  const res = await mockApiRequest(() => {
    return paginateData(allMockData, pageNo, pageSize, {})
  }, 500)
  if (route.query.a) {
    res.records.forEach((d, i) => {
      if (i == 9) {
        d.isImg = false
        d.src = img10
      } else if (i == 5 || i == 6) {
        d.isImg = true
        d.src = images[i]
      } else {
        d.isImg = true
        d.src = images[i]
      }
      d.avatar = avatar
    })
  } else {
    res.records.forEach((d, i) => {
      if (i == 9) {
        d.isImg = false
        d.src = img10
      } else if (i == 5 || i == 6) {
        d.isImg = true
        d.src = images[i]
      } else {
        d.isImg = true
        d.src = images[i]
      }
      d.avatar = avatar
    })
  }
  paging.value.complate(res)
};
</script>

<style lang="scss" scoped>
.card {
  display: flex;
  flex-direction: column;
}

.card_img {
  overflow: hidden;
}

.card_video {
  width: 100%;
  object-fit: contain;
  overflow: hidden;
}
</style>